class Player {
    constructor(name,teamColor) {
        this.name = name;
        // this.enemy = [];
        // this.partner = [];
        this.stats = 'live';
        this.teamColor = teamColor;
    }
    win() {
        console.log(this.name,"win---");
    }
    lose() {
        console.log(this.name,"lose---");
    }
    die() {
        this.stats = 'die';
        playerDirector.receiveMessage('playerDie',this)
    }
    remove() {
        playerDirector.receiveMessage('playerRemove',this)
    }
    changeTeam(color) {
        playerDirector.receiveMessage('changeTeam',this,color)
    }
}

var players = [];
var playerFactory = function (name, teamColor) {
    var newPlayer = new Player(name,teamColor)
    playerDirector.receiveMessage('addPlayer',newPlayer)
    return newPlayer
}
// 接下来定义中介者，一般有两种模式
//1.
//❏ 利用发布—订阅模式。将playerDirector实现为订阅者，各player作为发布者，
//一旦player的状态发生改变，便推送消息给playerDirector,playerDirector处理消息后将反馈发送给其他player。
//2.
//❏ 在playerDirector中开放一些接收消息的接口，各player可以直接调用该接口来给playerDirector发送消息，
//player只需传递一个参数给playerDirector，这个参数的目的是使playerDirector可以识别发送者。
//同样，playerDirector接收到消息之后会将处理结果反馈给其他player。
// 作者在这里使用第二种，其实第一种和第二种没什么太大区别

var playerDirector = (function () {

    var players = {},
        operation = {};
    
    operation.addPlayer = function (player) {
        var color = player.teamColor;
        players[color] = players[color] || [];
        players[color].push(player)
    }

    operation.playerRemove = function (player) {
        var color = player.teamColor;
        var playerList = players[color];
        for (let i = 0; i < playerList.length; i++) {
            if(playerList[i] === player) {
                playerList.splice(i,1)
            }
        }
        // console.log(playerList);
    }

    operation.playerDie = function (player) {
        var all_die = true;
        player.stats = 'die';
        var playerList = players[player.teamColor]
        for(let i = 0 , g; g = playerList[i++];){
            if(g.stats !== 'die'){
                all_die = false;
                break;
            }
        }
        if(all_die){
            for(let i = 0 , g; g = playerList[i++];){
                g.lose()
            }
            for (const k in players) {
                if(k !== player.teamColor){
                    for(let i = 0 , g; g = players[k][i++];){
                        g.win()
                    }
                }
            }
        }
    }
    operation.changeTeam = function (player,color) {
        operation.playerRemove(player);
        player.teamColor = color;
        operation.addPlayer(player);
    }

    var receiveMessage = function () {
        var args = Array.prototype.shift.apply(arguments)
        operation[args].apply(this,arguments)
    }

    return {
        receiveMessage : receiveMessage
    }
})()

//红队
var player1 = playerFactory("张俊杰1",'red')
var player2 = playerFactory("张俊杰2",'red')
var player3 = playerFactory("张俊杰3",'red')
var player4 = playerFactory("张俊杰4",'red')
// 蓝队
var player5 = playerFactory("张俊杰5",'blue')
var player6 = playerFactory("张俊杰6",'blue')
var player7 = playerFactory("张俊杰7",'blue')
var player8 = playerFactory("张俊杰8",'blue')

// player1.die()
// player2.die()
// player3.die()
// player4.die()

// player1.remove()
// player2.remove()
// player3.die()
// player4.die()

player1.changeTeam()
player2.die()
player3.die()
player4.die()



